package fr.didier.template.lesobjets;
/*
 * 
 * FC PSelectedElement.java
 * Flowchart Plotter
 * Ryan Newman, Clint Riley
 * 
 * http://code.google.com/p/flowchartplotter/

 */
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.Vector;

import fr.didier.template.Constantes;

/**
 * Used to store elements that are currently selected in the document
 * @author Clint Riley, Ryan Newman
 *
 */
public class ObjetSelected {
	public static final int HANDLE_NW = 0;
	public static final int HANDLE_N = 1;
	public static final int HANDLE_NE = 2;
	public static final int HANDLE_W = 3;
	public static final int HANDLE_E = 4;
	public static final int HANDLE_SW = 5;
	public static final int HANDLE_S = 6;
	public static final int HANDLE_SE = 7;
	
	public Objets s;
	Collection<Shape> handles = new Vector<Shape>();
	
	Shape nw, no, ne, we, ea, sw, so, se;
	
	public ObjetSelected(Objets s) {
		this.s = s;
		generateHandles();
	}
	
	public int getHandleClicked(Shape s) {
		if(s == nw) return ObjetSelected.HANDLE_NW;
		if(s == no) return ObjetSelected.HANDLE_N;
		if(s == ne) return ObjetSelected.HANDLE_NE;
		if(s == we) return ObjetSelected.HANDLE_W;
		if(s == ea) return ObjetSelected.HANDLE_E;
		if(s == sw) return ObjetSelected.HANDLE_SW;
		if(s == so) return ObjetSelected.HANDLE_S;
		if(s == se) return ObjetSelected.HANDLE_SE;
		return -1;
	}
	
	public Collection<Shape> getHandles() {
		return handles;
	}
	
	public void generateHandles() {
		int handleWidth = 7;
		int handleHeight = 7;
		
	//	LaLigne garbage = new LaLigne(0, 0, 0, "", 0, 0, null);
		Rectangle2D bounds = s.getBounds();
		sw = new Rectangle2D.Double(bounds.getMinX()- handleWidth, bounds.getMaxY(), handleWidth, handleHeight);
		handles.add(sw);
		ne = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMinY()-handleHeight, handleWidth, handleHeight);
		handles.add(ne);
		//if(s.getClass() == garbage.getClass()){
		if (false){	
		if(bounds.getMinX() == s.getX1() && (int)bounds.getMinY()  == s.getY1()){
				handles.remove(ne);
				handles.remove(sw);
				Constantes.debugMessage("x1,y1 in top left");
				nw = new Rectangle2D.Double(bounds.getMinX() - handleWidth, bounds.getMinY() - handleHeight, handleWidth, handleHeight);
				handles.add(nw);
				se = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMaxY(), handleWidth, handleHeight);
				handles.add(se);
			}else
			if((int)bounds.getMinX() == s.getX2() && (int)bounds.getMinY()  == s.getY2()){
				handles.remove(ne);
				handles.remove(sw);
				Constantes.debugMessage("x2,y2 in top left");
				nw = new Rectangle2D.Double(bounds.getMinX() - handleWidth, bounds.getMinY() - handleHeight, handleWidth, handleHeight);
				handles.add(nw);
				se = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMaxY(), handleWidth, handleHeight);
				handles.add(se);
			}else
			if((int)bounds.getMinX() == s.getX1() && (int)bounds.getMaxY()  == s.getY1()){
				handles.remove(ne);
				handles.remove(sw);
				Constantes.debugMessage("x1,y1 in bottom left");
				sw = new Rectangle2D.Double(bounds.getMinX()- handleWidth, bounds.getMaxY(), handleWidth, handleHeight);
				handles.add(sw);
				ne = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMinY()-handleHeight, handleWidth, handleHeight);
				handles.add(ne);
			}else
			if((int)bounds.getMinX() == s.getX2() && (int)bounds.getMaxY()  == s.getY2()){
				handles.remove(ne);
				handles.remove(sw);
				Constantes.debugMessage("x2,y2 in bottom left");	
				sw = new Rectangle2D.Double(bounds.getMinX()- handleWidth, bounds.getMaxY(), handleWidth, handleHeight);
				handles.add(sw);
				ne = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMinY()-handleHeight, handleWidth, handleHeight);
				handles.add(ne);
			}	
		}else{
		
		
		nw = new Rectangle2D.Double(bounds.getMinX() - handleWidth, bounds.getMinY() - handleHeight, handleWidth, handleHeight);
		handles.add(nw);
		no = new Rectangle2D.Double(bounds.getCenterX()- handleWidth* .5, bounds.getMinY() - handleHeight, handleWidth, handleHeight);
		handles.add(no);
		ne = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMinY()-handleHeight, handleWidth, handleHeight);
		handles.add(ne);
		we = new Rectangle2D.Double(bounds.getMinX()-handleWidth, bounds.getCenterY()-handleHeight * .5, handleWidth, handleHeight);
		handles.add(we);
		ea = new Rectangle2D.Double(bounds.getMaxX(), bounds.getCenterY()- handleHeight * .5 , handleWidth, handleHeight);
		handles.add(ea);
		sw = new Rectangle2D.Double(bounds.getMinX()- handleWidth, bounds.getMaxY(), handleWidth, handleHeight);
		handles.add(sw);
		so = new Rectangle2D.Double(bounds.getCenterX()-handleWidth * .5, bounds.getMaxY(), handleWidth, handleHeight);
		handles.add(so);
		se = new Rectangle2D.Double(bounds.getMaxX(), bounds.getMaxY(), handleWidth, handleHeight);
		handles.add(se);
		}
	}

	public void drawHandles(Graphics2D g2) {
		g2.setColor(Color.black);
		for(Shape s : handles) {
			g2.draw(s);
		}
		
	}
	
	public void updateHandles() {
		handles.clear();
		generateHandles();
	}
	
	public int getFixedHeight(Shape sa) {
		return (int)(s.getBounds().getMaxY() - s.getBounds().getMinY());
	}
	
	public int getFixedWidth(Shape s) {
		return(int)(s.getBounds().getMaxX() - s.getBounds().getMinX());
	}
	
	public boolean getFixedHeightBool(Shape s) {
		return (s == we || s == ea);
	}
	
	public boolean getFixedWidthBool(Shape s) {
		return (s == no || s == so);
	}
	
	public int getFixedY(Shape s2) {
		if(s2 == nw || s2 == ne 
		   || s2 == no || s2 == we) { 
		
			return (int)s.getBounds().getMaxY();
		}
		return (int)s.getBounds().getMinY();
	}
	
	public int getFixedX(Shape s2) {
		if(s2 == nw || s2 == sw || s2 == we) return (int)s.getBounds().getMaxX();
		//if(s2 == ne || s2 == se || se == ea || s2 == so) return (int)s.getBounds().getMinX();
		return (int)s.getBounds().getMinX();
		//return -1;
	}
	
}
